#!/bin/sh
#获取脚本所在目录
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

#镜像名
IMG_NAME="swotxu/screw-web"
#容器名
CON_NAME="swotxu-screw-web"
#文件挂载目录
DOCKER_VOLUME="/data/docker-data/xuhai/screw"


#使用说明，用来提示输入参数
usage() {
    echo "Usage: sh 执行脚本.sh [build|run|status|start|stop|restart|rm|rmi|logs] [port|jarname] [version]"
    exit 1
}

#依据Dockerfile创建镜像
#arg1: jar文件名
#arg2: 镜像版本，如 5.7，不写版本默认latest
build(){
  image_version=${2-latest}
  # 检查镜像版本是否已经存在（允许存在latest版本）
  if [ $image_version != "latest" ]; then
    image_id=`docker images |grep $IMG_NAME |grep $image_version |awk '{print $3}' `
    if [ -n "${image_id}" ]; then
      echo ">>> Docker image $IMG_NAME:$image_version is already exist <<<" 
      return 0
    fi
  fi
  docker build --build-arg JAR_NAME=$1 -t $IMG_NAME:$image_version $DIR/.
  echo ">>> Docker image $IMG_NAME:$image_version building a successful <<<" 
}

#依据镜像启动dockers容器
#arg1: 映射端口
#arg2: 镜像版本,不写版本默认latest
run(){
  container_name=$CON_NAME-$1
  # 检查是否已存在同名容器
  is_exist_docker $container_name
  if [ $? -eq "1" ]; then
    echo ">>> Docker container $container_name is already exist <<<" 
    return 0
  fi
  image_version=${2-latest}
  # 检查镜像是否存在
  image_id=`docker images |grep $IMG_NAME |grep $image_version |awk '{print $3}' `
  if [ -z "${image_id}" ]; then
    echo ">>> Docker image $IMG_NAME:$image_version is not exist <<<" 
    return 0
  fi
  # 启动容器
  docker run -di -p $1:8849 --name=$container_name \
    -v $DOCKER_VOLUME/conf:/screw/conf \
    -v $DOCKER_VOLUME/logs/$1:/screw/logs \
    -v $DOCKER_VOLUME/doc:/screw/doc \
    -d $IMG_NAME:$image_version
}

#检查docker容器是否存在
#arg1: 容器名(full)
#return: 1-存在 0-不存在
is_exist_docker(){
  container_id=`docker ps -a |grep $1 |awk '{print $1}' `   
  if [ -n "${container_id}" ]; then
   return 1
  else
    return 0
  fi
}

#检查docker容器是否运行
#arg1: 容器名(full)
#return: 1-运行中 0-未运行
is_exist_docker_run(){
  container_id=`docker ps |grep $1 |awk '{print $1}' `
  if [ -n "${container_id}" ]; then
   return 1
  else
    return 0
  fi
}

#获取docker容器运行状态
#arg1: 容器名后缀
status_docker(){
  container_name=$CON_NAME-$1
  is_exist_docker_run $container_name
  if [ $? -eq "1" ]; then
    echo ">>> Docker $container_name is running <<<"
  else
    echo ">>> Docker $container_name is not running <<<"
  fi
}

#启动指定docker容器
#arg1: 容器名后缀
start_docker(){
  container_name=$CON_NAME-$1
  is_exist_docker_run $container_name
  if [ $? -eq "1" ]; then 
    echo ">>> Docker $container_name is already running <<<" 
  else 
    docker start $container_name
    echo ">>> start $container_name successed <<<" 
  fi
}

#停止指定docker容器
#arg1: 容器名后缀
stop_docker(){
  container_name=$CON_NAME-$1
  is_exist_docker_run $container_name
  if [ $? -eq "1" ]; then 
    docker stop $container_name
    is_exist_docker_run $container_name
    if [ $? -eq "1" ]; then 
      echo ">>> Docker $container_name service stop failed!!!!! <<<"
    fi
  else 
    echo ">>> Docker $container_name service not started <<<" 
  fi
}

#删除指定容器
#arg1: 容器名后缀
rm_docker(){
  container_name=$CON_NAME-$1
  # 检查容器是否存在
  is_exist_docker $container_name
  if [ $? -eq "0" ]; then 
    echo ">>> Docker container $container_name is not exist <<<" 
    return 0
  fi
  stop_docker $1
  # 检查并停止容器
  stop_docker $1
  # 删除容器
  docker rm $container_name
}

#删除指定版本的镜像
#arg1: 镜像版本，如 5.7，不写版本默认latest
rmi_docker(){
  image_version=${1-latest}
  # 停止镜像对应的所有容器
  docker stop ${docker ps | grep $IMG_NAME:$image_version |awk '{print $1}'}
  # 删除镜像对应的所有容器
  docker rm ${docker ps -a| grep $IMG_NAME:$image_version |awk '{print $1}'}
  # 删除容器
  docker rmi $IMG_NAME:$image_version
}

#查看指定容器日志
#arg1: 容器名后缀
log_docker(){
  container_name=$CON_NAME-$1
  is_exist_docker $container_name
  if [ $? -eq "0" ]; then 
    echo ">>> Docker container $container_name is not exist <<<" 
  else 
    docker logs -f --tail=500 $container_name
  fi
}

#进入指定容器
#arg1: 容器名后缀
exec_docker(){
  container_name=$CON_NAME-$1
  is_exist_docker_run $container_name
  if [ $? -eq "0" ]; then 
    echo ">>> Docker container $container_name is not start <<<" 
  else 
    docker exec -it $container_name /bin/bash
  fi
}

#重启
restart_docker(){
  stop_docker $1
  start_docker $1
}


#根据输入参数，选择执行对应方法，不输入则执行使用说明
case "$1" in
  "build")
    build $2 $3
    ;;
  "run")
    run $2 $3
    ;;
  "start")
    start_docker $2
    ;;
  "stop")
    stop_docker $2
    ;;
  "status")
    status_docker $2
    ;;
  "restart")
    restart_docker
    ;;
  "logs")
    log_docker $2
    ;;
  "exec")
    exec_docker $2
    ;;
  "rm")
    rm_docker $2
    ;;
  "rmi")
    rmi_docker $2
    ;;
  *)
    usage
    ;;
esac
exit 0